﻿using LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class VirtualKeyboardPopup : Popup
    {
        public VirtualKeyboardPopup()
        {
            this.Focusable = false;

            var size = App.ConfigManager.Get("VirtualKeyboardSize");
            if (string.IsNullOrWhiteSpace(size) == false)
            {
                switch (size)
                {
                    case "Big":
                        {
                            this.Width = 962;
                            this.Height = 362;
                            keyboard.btnBig.IsChecked = true;
                            break;
                        }
                    case "Middle":
                        {
                            this.Width = 722;
                            this.Height = 272;
                            keyboard.btnMiddle.IsChecked = true;
                            break;
                        }
                    default:
                        {
                            this.Width = 500;
                            this.Height = 222;
                            keyboard.btnLittle.IsChecked = true;
                            break;
                        }
                }
            }
            else
            {
                this.Width = 500;
                this.Height = 222;
            }

            this.Child = new Border()
            {
                Child = keyboard,
                BorderThickness = new System.Windows.Thickness(1),
                BorderBrush = Brushes.SaddleBrown,
                SnapsToDevicePixels = true,
            };

            keyboard.KeyPressed += Keyboard_KeyPressed;
        }

        private void Keyboard_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            if (e.KeyText == "Esc" || e.KeyText == "Close")
            {
                this.IsOpen = false;
                return;
            }

            OnKeyPressed(sender, e);
        }

        private readonly VirtualKeyboard keyboard = new VirtualKeyboard()
        {
            HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
        };

        public VirtualKeyboard Keyboard => keyboard;

        public event EventHandler<KeyPressedEventArgs> KeyPressed;

        protected void OnKeyPressed(object sender, KeyPressedEventArgs e)
        {
            KeyPressed?.Invoke(sender, e);
        }
    }
}
